package com.cangxuan.backend.controller;

import com.alibaba.fastjson.JSONObject;
import com.cangxuan.backend.annotation.Check;
import com.cangxuan.backend.annotation.check.Length;
import com.cangxuan.backend.annotation.check.NotEmpty;
import com.cangxuan.backend.entity.User;
import com.cangxuan.backend.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@Api(tags = "其它接口")
@Slf4j
@RestController
public class IndexController extends BaseController {

    IUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 登录参数
     */
    @Data
    @ApiModel("登录参数实体")
    public static class UserLoginBody {
        @ApiModelProperty("手机号，11位")
        @NotEmpty(message = "请传递mobile")
        private String mobile;
        @ApiModelProperty("短信验证码")
        @NotEmpty(message = "请传递vcode")
        @Length(size = 4, message = "传递的vcode长度必须为4位")
        private String vcode;
        @ApiModelProperty("手机的设备唯一编号")
        @NotEmpty(message = "请传递deviceId")
        private String deviceId;    //手机设备编号
        @ApiModelProperty("Jpush注册ID")
        @NotEmpty(message = "请传递registrationId")
        private String registrationId;    //手机设备编号
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    public Object login(@Check @RequestBody UserLoginBody userLoginBody) {
        User user = userService.login(userLoginBody.getMobile(), userLoginBody.getVcode(), userLoginBody.getDeviceId(), userLoginBody.getRegistrationId());
        JSONObject object = new JSONObject();
        object.put("id", user.getId());
        object.put("mobile", user.getMobile());
        object.put("blockchainAddress", user.getBlockchainAddress());
        object.put("nickname", user.getNickname());
//        object.put("face", user.getFace());
        object.put("hasSetTransPassword", user.getTransPassword() != null && !user.getTransPassword().equals(""));
        object.put("hasSetRealname", user.getRealName() != null && !user.getRealName().equals(""));
        object.put("subscribeToTheSalesCalendar", user.isSubscribeToTheSalesCalendar());
        object.put("displayMyGoods", user.isDisplayMyGoods());
        object.put("displayMySellGoods", user.isDisplayMySellGoods());
        return success(object);
    }

    @PostMapping("checkLogin")
    @ApiOperation("判断用户登录")
    public Object checkLogin() {
        User user = userService.getCurrentUser();
        JSONObject object = new JSONObject();
        object.put("id", user.getId());
        object.put("mobile", user.getMobile());
        object.put("blockchainAddress", user.getBlockchainAddress());
        object.put("nickname", user.getNickname());
//        object.put("face", user.getFace());
        object.put("hasSetTransPassword", user.getTransPassword() != null && !user.getTransPassword().equals(""));
        object.put("hasSetRealname", user.getRealName() != null && !user.getRealName().equals(""));
        object.put("subscribeToTheSalesCalendar", user.isSubscribeToTheSalesCalendar());
        object.put("displayMyGoods", user.isDisplayMyGoods());
        object.put("displayMySellGoods", user.isDisplayMySellGoods());
        return success(object);
    }

    RedisTemplate<String, Object> rateRedisTemplate;
    @PostMapping("logout")
    @ApiOperation("用户退出登录")
    public Object logout(@Check @RequestBody UserLoginBody userLoginBody){
        Object DeviceId = userService.logout(userLoginBody.getDeviceId());
        rateRedisTemplate.opsForValue().getAndDelete(String.valueOf(DeviceId));
        return success();
    }
}
